<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>手写sleep函数</title>
  </head>
  <body>
    <script>
      const sleep = (n) =>
        new Promise((resolve, reject) => {
          setTimeout(resolve, n);
          if (!n) {
            reject(console.log("请输入延迟时间"));
          }
        });

      sleep(3000)
        .then(() => {
          console.log("成功");
        })
        .catch((err) => {
          console.log(err);
        })
        .finally(() => {
          console.log("sleep完成");
        });
    </script>
  </body>
</html>
